using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {
	public int maxHealth = 100;
	public int curHealth = 100;
	public bool showHealth = false;
	
	public float healthBarLength;
	
	
	// Use this for initialization
	void Start () {
		healthBarLength = 200;
	}
	
	// Update is called once per frame
	void Update () {
		AdjustCurrentHealth(0);
	}
	
	void OnGUI() {
		if(showHealth == true)
		{
			GUI.Box(new Rect(9,49,(maxHealth * 2) + 2,22),"");
			GUI.Box(new Rect(10,50,(healthBarLength),20), "");
			GUI.Label(new Rect(10,50,(maxHealth*2),20), name + " Health: " + curHealth + "/" + maxHealth);
		}
	}
	public void AdjustCurrentHealth(int adjust){
		curHealth += adjust;
			
		healthBarLength = (curHealth * 2);
		if(curHealth > maxHealth)
		{
			curHealth = maxHealth;
		}
		if(curHealth <= 0)
		{
			curHealth = 0;
			
			GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
			foreach(GameObject enemy in go)
			{	
				if(enemy.name == name)
				{
					//Targeting tar = (Targeting)enemy.gameObject.GetComponent("Targeting");
					//tar.DeselectTarget();
					Destroy(enemy);
				}
			}
			
		}
		if(maxHealth < 1)
		{
			maxHealth = 1;
		}
		
	}
	
	public void AdjustShowHealth(bool adjust)
	{
		showHealth = adjust;
	}
}

